home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Sas-PPC / samplelib1 / dispatch68k.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  7.8 KB  |  259 lines

  1. #define  _USEOLDEXEC_ 1
  2. #include <exec/types.h>
  3. #include <exec/nodes.h>
  4. #include <exec/memory.h>
  5. #include <exec/resident.h>
  6. #include <exec/libraries.h>
  7. #include <exec/execbase.h>
  8. #include <libraries/dos.h>
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11. #include <string.h>
  12. #include <dos/dostags.h>
  13. #include <PowerUP/PPCLib/Interface.h>
  14. #include <PowerUP/PPCLib/tasks.h>
  15. #include <PowerUP/PPCLib/ppc.h>
  16. #include <PowerUP/PPCLib/object.h>
  17. #include <PowerUP/PPCLib/message.h>
  18. #include <PowerUP/PPCDisslib/PPCDiss.h>
  19. #include <PowerUP/pragmas/ppc_pragmas.h>
  20. #include <PowerUP/clib/ppc_protos.h>
  21. #include <PowerUP/pragmas/ppcdiss_pragmas.h>
  22. #include <PowerUP/clib/ppcdiss_protos.h>
  23.  
  24. #define CallPPCFunction _CallPPCFunction
  25. #include "ppcdispatch.h"
  26. #undef CallPPCFunction
  27.  
  28. static void *ReplyPort;
  29. static void *M68kPort;
  30. static void *Task;
  31. void *StartupMsg;
  32. struct StartupData    *StartupData;
  33.  
  34. static struct PPCDispatchMsg *DispatchMsg;
  35.  
  36. struct MyLibrary {
  37.         struct             Library ml_Lib;
  38.         ULONG              ml_SegList;
  39.         ULONG              ml_Flags;
  40.         APTR               ml_ExecBase; /* pointer to exec base  */
  41. #ifndef ONE_GLOBAL_SECTION
  42.         long *             ml_relocs;   /* pointer to relocs.    */
  43.         struct MyLibrary * ml_origbase; /* pointer to original library base  */
  44.         long               ml_numjmps;
  45. #endif
  46.         void *             ml_MyObject; /* pointer to ELF Object */
  47.         long               ml_ElfEntry; /* Entrypoint of Elf stub */
  48.  
  49. };
  50.  
  51.  
  52. void *PPCPort;
  53. struct Library *PPCLibBase;
  54.  
  55. int  __saveds __asm __libfpinit(register __a6 struct MyLibrary *libbase)
  56. {
  57.     static struct TagItem MyTags[17];
  58.     struct DOSLibrary *DOSBase;
  59.  
  60.     DOSBase = (void *)OpenLibrary("dos.library", 0);
  61.     if (DOSBase == NULL) return -1;
  62.  
  63.     if ((PPCLibBase = OpenLibrary("ppc.library", 0)) == NULL) 
  64.     {
  65.         CloseLibrary((void *)DOSBase);
  66.         return -1;
  67.     }
  68.  
  69.     MyTags[0].ti_Tag    =    TAG_DONE;
  70.     if (ReplyPort = PPCCreatePort(MyTags))
  71.     {
  72.         if (M68kPort = PPCCreatePort(MyTags))
  73.         {
  74.             if (StartupMsg = PPCCreateMessage(ReplyPort, sizeof(struct StartupData)))
  75.             {
  76.                 if (StartupData = PPCAllocVec(sizeof(struct StartupData), MEMF_ANY))
  77.                 {
  78.                     
  79.                     StartupData->MsgPort    = M68kPort;
  80.  
  81.                     MyTags[0].ti_Tag        =        PPCTASKTAG_ARG1;
  82.                     MyTags[0].ti_Data       =        (ULONG) 0;
  83.  
  84.                     MyTags[1].ti_Tag        =        PPCTASKTAG_ARG2;
  85.                     MyTags[1].ti_Data       =        (ULONG) _WBenchMsg;
  86.  
  87.                     MyTags[2].ti_Tag        =        PPCTASKTAG_ARG3;
  88.                     MyTags[2].ti_Data       =        (ULONG) libbase;
  89.  
  90.                     MyTags[3].ti_Tag        =        PPCTASKTAG_ARG4;
  91.                     MyTags[3].ti_Data       =        (ULONG) DispatchFunction;
  92.  
  93.                     MyTags[4].ti_Tag        =        PPCTASKTAG_STARTUP_MSG;
  94.                     MyTags[4].ti_Data       =(ULONG) StartupMsg;
  95.  
  96.                     MyTags[5].ti_Tag        =        PPCTASKTAG_STARTUP_MSGDATA;
  97.                     MyTags[5].ti_Data       =(ULONG) StartupData;
  98.                 
  99.                     MyTags[6].ti_Tag        =        PPCTASKTAG_STARTUP_MSGLENGTH;
  100.                     MyTags[6].ti_Data       =        sizeof(StartupData);
  101.  
  102.                     MyTags[7].ti_Tag        =        PPCTASKTAG_STARTUP_MSGID;
  103.                     MyTags[7].ti_Data       =        0;
  104.  
  105.                     MyTags[8].ti_Tag        =        PPCTASKTAG_STACKSIZE;
  106.                     MyTags[8].ti_Data       =        0x10000;
  107.  
  108.                     MyTags[9].ti_Tag        =        PPCTASKTAG_MSGPORT;
  109.                     MyTags[9].ti_Data       =        TRUE;
  110.  
  111.                     MyTags[10].ti_Tag       =        PPCTASKTAG_INPUTHANDLE;
  112.                     MyTags[10].ti_Data      =        (ULONG) Input();
  113.                     
  114.                     MyTags[11].ti_Tag       =        PPCTASKTAG_OUTPUTHANDLE;
  115.                     MyTags[11].ti_Data      =        (ULONG) Output();
  116.  
  117.                     MyTags[12].ti_Tag       =        NP_CloseInput;
  118.                     MyTags[12].ti_Data      =        FALSE;
  119.  
  120.                     MyTags[13].ti_Tag       =        NP_CloseOutput;
  121.                     MyTags[13].ti_Data      =        FALSE;
  122.  
  123.                     MyTags[14].ti_Tag       =        PPCTASKTAG_BREAKSIGNAL;
  124.                     MyTags[14].ti_Data      =        TRUE;
  125.  
  126.                     MyTags[15].ti_Tag       =        PPCTASKTAG_STOPTASK;
  127.                     MyTags[15].ti_Data      =        FALSE;
  128.                     
  129.                     MyTags[16].ti_Tag       =        TAG_END;
  130.     
  131.                     if (Task = PPCCreateTask(libbase->ml_MyObject, &MyTags[0]))
  132.                     {
  133. Delay(1); /* seems to help a bit, but doesn't totally solve the problem */
  134.           /* with V45.20 of ppc.library */
  135.                         if (PPCPort=(void*) PPCGetTaskAttrsTags(Task,
  136.                                                             PPCTASKINFOTAG_MSGPORT,0,
  137.                                                             TAG_END))
  138.                         {
  139.                             CloseLibrary((void *)DOSBase);
  140.                             return 0;  /* everything is set up */
  141.                         }
  142.                     }
  143.                     PPCFreeVec(StartupData);
  144.                 }
  145.                 PPCDeleteMessage(StartupMsg);
  146.             }
  147.             PPCDeletePort(M68kPort);
  148.         }
  149.         PPCDeletePort(ReplyPort);
  150.     }
  151.     CloseLibrary(PPCLibBase);
  152.     CloseLibrary((void *)DOSBase);
  153.  
  154.     return -1; /* failed */
  155. }
  156.  
  157.  
  158. __saveds long CallPPCFunction(long (*f)(...), long r3, long r4, long r5, long r6, 
  159.                      long r7, long r8, long r9, long r10)
  160. {
  161.     long *ret;
  162.     void *M68kMsg;
  163.     struct PPCDispatchMsg *DispatchMsg;
  164.     void *PPCMsg;
  165.     long value;
  166.     
  167.     PPCMsg = PPCCreateMessage(ReplyPort, sizeof(*DispatchMsg));
  168.     DispatchMsg = PPCAllocVec(sizeof(struct PPCDispatchMsg), MEMF_ANY);
  169.     
  170.  
  171.     DispatchMsg->func = f;
  172.     DispatchMsg->r3  = r3;
  173.     DispatchMsg->r4  = r4;
  174.     DispatchMsg->r5  = r5;
  175.     DispatchMsg->r6  = r6;
  176.     DispatchMsg->r7  = r7;
  177.     DispatchMsg->r8  = r8;
  178.     DispatchMsg->r9  = r9;
  179.     DispatchMsg->r10 = r10;
  180.     
  181.     PPCSendMessage(PPCPort, PPCMsg, DispatchMsg,
  182.                    sizeof(*DispatchMsg), 0x12345678);
  183.  
  184.     
  185.     while(PPCGetMessage(ReplyPort) == NULL)
  186.        PPCWaitPort(ReplyPort);
  187.  
  188.  
  189.     PPCFreeVec(DispatchMsg);
  190.     PPCDeleteMessage(PPCMsg);
  191.     
  192.     while ((M68kMsg = PPCGetMessage(M68kPort)) == NULL)
  193.        PPCWaitPort(M68kPort);
  194.  
  195.     ret = (void *)PPCGetMessageAttr(M68kMsg, PPCMSGTAG_DATA);
  196.  
  197.     value = *ret;
  198.     PPCReplyMessage(M68kMsg);
  199.     return value;
  200.   
  201. }
  202.  
  203.  
  204. #if 0
  205.  
  206. __saveds __asm void __UserLibTerm(register __a6 struct MyLibrary *libbase)
  207. {
  208.     DispatchMsg->func = NULL;
  209.     
  210.     PPCSendMessage(PPCPort, StartupMsg, DispatchMsg,
  211.                    sizeof(*DispatchMsg), 0x12345678);
  212.  
  213.     
  214.     PPCWaitPort(ReplyPort);
  215.  
  216.     PPCFreeVec(DispatchMsg);
  217.     PPCDeleteMessage(StartupMsg);
  218.     PPCDeletePort(M68kPort);
  219.     PPCDeletePort(ReplyPort);
  220.  
  221.     CloseLibrary(PPCLibBase);
  222. }
  223.  
  224. #endif
  225.  
  226.  
  227. void __saveds __asm __libfpterm(register __a6 struct MyLibrary *libbase)
  228. {
  229.     struct PPCDispatchMsg *DispatchMsg;
  230.     void *PPCMsg;
  231.     
  232.     PPCMsg = PPCCreateMessage(ReplyPort, sizeof(*DispatchMsg));
  233.     DispatchMsg = PPCAllocVec(sizeof(struct PPCDispatchMsg), MEMF_ANY);
  234.     
  235.     DispatchMsg->func = NULL;
  236.     
  237.     PPCSendMessage(PPCPort, PPCMsg, DispatchMsg,
  238.                    sizeof(*DispatchMsg), 0x12345678);
  239.  
  240.     while(PPCGetMessage(ReplyPort) != StartupMsg)
  241.     {
  242.         PPCWaitPort(ReplyPort);
  243.     }
  244.     
  245.  
  246.     PPCFreeVec(DispatchMsg);
  247.     PPCDeleteMessage(PPCMsg);
  248.  
  249.     PPCDeleteMessage(StartupMsg);
  250.     PPCFreeVec(StartupData);
  251.     PPCDeletePort(M68kPort);
  252.     PPCDeletePort(ReplyPort);
  253.  
  254.     CloseLibrary(PPCLibBase);
  255.  
  256. }
  257.  
  258.  
  259.